package org.springblade.modules.repair.vo.plan;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainPlanTemplate;
import org.springblade.modules.repair.enums.ElevatorStatusEnum;
import org.springblade.modules.repair.enums.EmerProcTypeEnum;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.utils.DateUtils;
import org.springblade.modules.repair.vo.DateResultVo;
import org.springblade.modules.repair.vo.PlanDetailLocationTypeVO;
import org.springblade.modules.repair.vo.PlanTemplateVO;
import org.springblade.modules.repair.vo.plan.v2.PlanUserSignDetailVo;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zcc
 * @version 1.0
 * @date 2021/10/16 9:51
 */
@ApiModel("维保任务电梯信息")
@Data
public class PlanContentElevatorVo implements Comparable<PlanContentElevatorVo> {


	@ApiModelProperty("小区名称")
	private String buildingName;

	@ApiModelProperty("小区ID")
	private Long buildingId;

	@ApiModelProperty(value = "经度")
	private String longitude;

	@ApiModelProperty(value = "纬度")
	private String latitude;

	@ApiModelProperty("任务ID")
	private Long planId;

	@ApiModelProperty("电梯ID")
	private Long elevatorId;

	@ApiModelProperty("电梯地址")
	private String elevatorAddress;

	private String buildingAddress;

	@ApiModelProperty("任务类型1:保养，2维修，3：困人")
	private Integer typeStatus;

	@ApiModelProperty("任务类型名称 1:保养，2维修，3：困人")
	private String typeStatusName;

	@ApiModelProperty("任务时间")
	private String planDate;

	@ApiModelProperty("状态1待处理，2进行中，3:提交任务 4已完成")
	private Integer status;

	@ApiModelProperty("状态1待处理，2进行中，3:提交任务 4已完成")
	private String statusName;

	@ApiModelProperty("维修预警的状态")
	private String userWxArriveStatus;

	private String planAarriveDates;

	@ApiModelProperty("北京时间")
	private Date beijingDate = DateUtils.beijingDate();

	public void setPlanAarriveDates(String planAarriveDates) {
		if(StrUtil.isNotEmpty(planAarriveDates)&& planAarriveDates.contains("%")){
			List<String> strings = Func.toStrList(planAarriveDates);
			List<JSONObject> jsonObjects = new ArrayList<>(16);
			for (String str : strings) {
				String[] split = str.split("%");
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("user_id",split[0]);
				jsonObject.put("planArriveDate",split[1]);
				jsonObjects.add(jsonObject);
			}
			this.planAarriveDates = JSON.toJSONString(jsonObjects);
		}else{
			this.planAarriveDates = planAarriveDates;
		}
	}

	public void setUserWxArriveStatus(String userWxArriveStatus) {
		if(StrUtil.isNotEmpty(userWxArriveStatus)&& userWxArriveStatus.contains("%")){
			List<String> strings = Func.toStrList(userWxArriveStatus);
			List<JSONObject> jsonObjects = new ArrayList<>(16);
			for (String str : strings) {
				String[] split = str.split("%");
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("user_id",split[0]);
				jsonObject.put("status",split[1]);
				jsonObjects.add(jsonObject);
			}
			this.userWxArriveStatus = JSON.toJSONString(jsonObjects);
		}else{
			this.userWxArriveStatus = userWxArriveStatus;
		}
	}

	public void setPlanCheckName(String planCheckName) {
		if (Func.isNotEmpty(planCheckName) && planCheckName.contains("%")) {
			List<String> strings = Func.toStrList(planCheckName);
			List<MaintainPlanTemplate> list = new ArrayList<>(10);
			for (String str : strings) {
				String[] split = str.split("%");
				MaintainPlanTemplate maintainPlanTemplate = new MaintainPlanTemplate();
				maintainPlanTemplate.setTemplateName(split[1]);
				maintainPlanTemplate.setSort(Convert.toInt(split[2], 0));
				list.add(maintainPlanTemplate);
			}
			List<String> collect = list.stream().sorted(Comparator.comparing(MaintainPlanTemplate::getSort)).map(MaintainPlanTemplate::getTemplateName).collect(Collectors.toList());
			this.planCheckName = String.join(",", collect);
		} else {
			this.planCheckName = planCheckName;
		}
	}

	@ApiModelProperty("保养名称")
	private String planCheckName;

	@ApiModelProperty("停梯状态： 1-正常 2-停梯")
	private Integer elevatorIsStop;

	@ApiModelProperty("停梯状态： 1-正常 2-停梯")
	private String elevatorIsStopName;

	@ApiModelProperty("维修故障原因编码")
	private String problemItem;

	@ApiModelProperty("维修故障原因")
	private List<String> problemItemNames;

	@ApiModelProperty("是否存在延迟")
	private Integer isDeferred;

	@ApiModelProperty("延期文案")
	private String deferredName;

	@ApiModelProperty("逾期文案")
	private String overdueName;

	@ApiModelProperty("完成时间")
	private Date finishTime;


	@ApiModelProperty("工作时长")
	private Integer workMinute;

	@ApiModelProperty("工作时长")
	private String workMinuteName;

	private String userIds;

	private String createUserName;

	private List<PlanUserSignDetailVo> signUsers;

	private String signInfo;

	@ApiModelProperty(value = "评分状态 0-未评分，1-已评分")
	private Integer scoreStatus;

	@ApiModelProperty(value = "评分分数")
	private BigDecimal score;

	@ApiModelProperty("排序权重")
	private Integer weight = 0;

	private String partsName;

	@ApiModelProperty("层站id")
	private String floorStandIds;

	@ApiModelProperty("层站名称")
	private String floorStandNames;

	private String partsNum;

	@ApiModelProperty("1:非年审，2:年审")
	private Integer auditType;

	@ApiModelProperty("电梯年审状态1:非年审，0:年审")
	private Integer elevatorAuditType;

	@ApiModelProperty("工单总签到人数")
	private Integer signUserCount;
	@ApiModelProperty("工单总签字维保人数")
	private Integer signatureMaintainUserCount;

	/**
	 * 最终延期类型
	 * 1：已延期，
	 * 2：已提前
	 * 3: 无变化
	 * 4: 从未调整 -默认状态
	 * 5：特殊延期
	 */
	private Integer deferType;


	@ApiModelProperty("救援类型")
	private Integer trapType;

	@ApiModelProperty("救援描述")
	private String trapDesc;

	private Date overdueDate;

	private Date maintainSignatureTime;

	private String timeliness;

	@ApiModelProperty("别名")
	private String aliasStr;


	@ApiModelProperty("事务工单的描述信息")
	private String affairDesc;

	/**
	 * 紧急程度
	 */
	private Integer emerProcType;

	private String emerProcTypeName;

	private String malPartsNames;

	@ApiModelProperty("维修层站id")
	private String malFloorStandIds;

	@ApiModelProperty("维修层站名称")
	private String malFloorStandNames;

	@ApiModelProperty("1:转派工单0:非转派工单")
	private Integer giveStatus;

	@ApiModelProperty(value = "1:保养质检，2:重复故障质检，3:维保移交质检4:安装移交质检5:常规质检")
	private Integer checkType;

	@ApiModelProperty(value = "工作项记录")
	private List<PlanTemplateVO> planTemplateList;

	@ApiModelProperty(value = "工作项类型统计")
	private Map<String, Object> planLocationTypeList;

	public void setPartsName(String partsName) {
		if (Func.isNotEmpty(partsName)) {
			this.partsName = partsName.replace("&amp;&amp;", ">");
		}
	}

	public void setFloorStandNames(String floorStandNames) {
		if (Func.isNotEmpty(floorStandNames)) {
			this.floorStandNames = floorStandNames.replace("&amp;&amp;", ">");
		}
	}

	public void setMalPartsNames(String malPartsNames) {
		if (Func.isNotEmpty(malPartsNames)) {
			this.malPartsNames = malPartsNames.replace("&amp;&amp;", ">");
		}
	}

	public void setMalFloorStandNames(String malFloorStandNames) {
		if (Func.isNotEmpty(malFloorStandNames)) {
			this.malFloorStandNames = malFloorStandNames.replace("&amp;&amp;", ">");
		}
	}

	public void setEmerProcType(Integer emerProcType) {
		if (Func.isNotEmpty(emerProcType)) {
			this.emerProcTypeName = EmerProcTypeEnum.getName(emerProcType);
		}
		this.emerProcType = emerProcType;
	}

	public void setIsDeferred(Integer isDeferred) {
		this.isDeferred = isDeferred;
		deferredName = isDeferred == 1 ? "延期保养" : "未延期保养";
	}

	public void setTypeStatus(Integer typeStatus) {
		this.typeStatus = typeStatus;
		this.typeStatusName = MaintainPlanContentEnum.getName(typeStatus);
	}


	public void setWorkMinute(Integer workMinute) {
		this.workMinute = workMinute;
		if (workMinute != null) {
			if (workMinute < 25) {
				this.workMinuteName = "时间过短";
			} else if (workMinute > 60) {
				this.workMinuteName = "时间过长";
			}
		}
	}

	public void setPlanDate(String planDate) {
		this.planDate = planDate;
		Date dateTime = DateUtil.parse(planDate + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		if (date.getTime() > dateTime.getTime()) {
			Integer days = Convert.toInt(DateUtil.between(dateTime, date, DateUnit.DAY));
			if (days > 0) {
				this.overdueName = "超时" + days + "天";
			}

		}

	}


	public void setElevatorIsStop(Integer elevatorIsStop) {
		this.elevatorIsStop = elevatorIsStop;
		this.elevatorIsStopName = ElevatorStatusEnum.getName(elevatorIsStop);
	}

	public void setSignInfo(String signInfo) {
		if (StrUtil.isNotEmpty(signInfo)) {
			signUsers = new ArrayList<>();
			String[] split = signInfo.split("%");
			for (String str : split) {
				PlanUserSignDetailVo planUserSignDetailVo = JSON.parseObject(str, PlanUserSignDetailVo.class);
				signUsers.add(planUserSignDetailVo);
			}
		}
		this.signInfo = signInfo;
	}


	@Override
	public int compareTo(PlanContentElevatorVo planContentElevatorVo) {
		return planContentElevatorVo.getWeight() - this.weight;
	}
}
